package pl.edu.agh.inpema;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import pl.edu.agh.inpema.core.environment.EnvironmentRunner;

public class PerformGatherAndAnalyze {

	private static final String LOGGER_PROP_FILE = "resources/logger.properties.xml";
	private static final String LOGGER_PROP_DEBUG_FILE = "resources/logger.properties.debug.xml";
	
	private static final String DEFAULT_CONFIGURATION_XML_FILE = "resources/configuration.xml";
	
	private static Set<String> possibleArgsSet = new HashSet<String>();
	
	static {
		possibleArgsSet.add("conf");
		possibleArgsSet.add("debug");
	}
	
	public static void main(String[] args) {
		
		Properties properties = null;
		try {
			properties = parseArgs(args);
		} catch (ParseException e) {
			System.err.println(e.getMessage());
			System.exit(0);
		}
		
		if ("true".equals(properties.getProperty("debug"))) {
			DOMConfigurator.configure(LOGGER_PROP_DEBUG_FILE);
			//HibernateUtil.setShowSql(true);
		} else {
			DOMConfigurator.configure(LOGGER_PROP_FILE);
			//HibernateUtil.setShowSql(false);
		}
		Logger logger = Logger.getLogger(PerformGatherAndAnalyze.class);
		
		logger.info("logger inicialized");
		logger.info("====");
		logger.info("  conf = "+properties.getProperty("conf"));
		logger.info("  debug = "+properties.getProperty("debug"));
		logger.info("====");
		
		EnvironmentRunner runner = new EnvironmentRunner();
		try {
			String xmlConf = "";
			String line;
			BufferedReader br = new BufferedReader(new FileReader(new File(properties.getProperty("conf"))));
			while ((line = br.readLine()) != null)
				xmlConf += line + "\n";
			
			logger.info("system start");
			
			runner.runNewEnvironment(xmlConf);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		logger.info("system end");
	}
	
	private static Properties parseArgs(String[] args) throws ParseException {
		Properties prop = new Properties();
		for (String arg : args) {
			String[] argParts = arg.split("=");
			if (argParts.length != 2) {
				throw new ParseException("wrong argument "+arg, 1);
			} else if (argParts[0].charAt(0) != '-') {
				throw new ParseException("wrong argument "+arg, 1);
			} else {
				String key = argParts[0].substring(1);
				String value = argParts[1];
				if (possibleArgsSet.contains(key))
					prop.setProperty(key, value);
				else
					throw new ParseException("wrong argument "+arg, 1);
			}
		}
		if (prop.getProperty("debug") == null) {
			prop.setProperty("debug", "false");
		} else {
			String debugValue = prop.getProperty("debug");
			if (! debugValue.equals("false") && ! debugValue.equals("true")) {
				throw new ParseException("Unknown value '"+debugValue+"'. Possible values for 'debug' option is 'true' or 'false'",1);
			}
		}
		if (prop.getProperty("conf") == null) {
			prop.setProperty("conf", DEFAULT_CONFIGURATION_XML_FILE);
		}
		return prop;
	}
}
